<?php
include_once ('class.mysql.php');

class RegisterOperation
{
    private $comtec_id_order;
    private $comtec_id_machine;
    private $comtec_id_responsible;
    private $operation_type;
    private $diagnosis;
    private $changed_parts;
    private $value_parts;
    private $value_work;
    private $value_others;
    private $value_total;
    private $notes;
    private $notes_for_owner;
    private $status_reg;
    private $delay;
    private $date_next_maintenance;
    private $status;

    private $errors;
    private $token;

    public function __construct()
    {
        $this->errors   = array();
        
        $this->comtec_id_order                  = $_POST['comtec_id_order'];
        $this->comtec_id_machine		= $_POST['comtec_id_machine'];
        $this->comtec_id_responsible	        = $_POST['comtec_id_responsible']; 
        $this->operation_type 			= $_POST['operation_type'];
        $this->operation_date_start             = $_POST['op_year1']."-".$_POST['op_month1']."-".$_POST['op_day1']." ".$_POST['hour1'].":".$_POST['minute1'].":00";
        $op_year2 = isset ($_POST['op_year2'])? $_POST['op_year2']:0;
        $op_month2 = isset($_POST['op_month2'])? $_POST['op_month2']:0;
        $op_day2 = isset ($_POST['op_day2'])? $_POST['op_day2']:0;
        if($op_year2 != 0 && $op_month2 != 0 && $op_day2 != 0){
         $this->operation_date_end              = $_POST['op_year2']."-".$_POST['op_month2']."-".$_POST['op_day2']." ".$_POST['hour2'].":".$_POST['minute2'].":00";
         $this->delay                           = $_POST['delay'];
        }else{
              $this->operation_date_end = 0;
              $this->delay = 0;
             }
        $this->diagnosis 			= $_POST['diagnosis'];
        if(strlen($this->diagnosis) > 400)
        {
         $this->diagnosis = substr($this->diagnosis,0,400);   
        }        
        $this->changed_parts			= $_POST['changed_parts'];
        if(strlen($this->changed_parts) > 400)
        {
         $this->changed_parts = substr($this->changed_parts,0,400);   
        }
        $this->value_parts			= isset ($_POST['value_parts'])? $_POST['value_parts']:0;
        $this->value_work			= isset ($_POST['value_work'])? $_POST['value_work']:0;
        $this->value_others			= isset ($_POST['value_others'])? $_POST['value_others']:0;
        $this->value_total			= $this->value_parts + $this->value_work + $this->value_others;
        $this->notes				= $_POST['notes'];
        if(strlen($this->notes) > 400)
        {
         $this->notes = substr($this->notes,0,400);   
        }
        $this->notes_for_owner			= isset ($_POST['notes_for_owner'])? $_POST['notes_for_owner']:"";
        if(strlen($this->notes_for_owner) > 400)
        {
         $this->notes_for_owner = substr($this->notes_for_owner,0,400);   
        }
        
        if($this->operation_type == 1){
         $this->date_next_maintenance            = $_POST['next_year'].$_POST['next_month'].$_POST['next_day']." 00:00:00"; 
        }
        
        $this->status                          = isset ($_POST['status'])? $_POST['status']:2;
        $this->status_reg			= 1;
        $this->token				= $_POST['token'];
    }

    public function process()
    {
	$this->register();

        return count($this->errors)? 0 : 1;
    }

    public function filter($var)
    {
        return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
    }

    public function filter_num($var)
    {
        return preg_replace('/[^0-9+]/','',$var);
    }

    public function register()
    {
        $db = new MySQL();

        $tsql = "INSERT INTO operation (
                                comtec_id_order,
                                comtec_id_machine,
                                comtec_id_responsible,
                                operation_type,
                                diagnosis,
                                changed_parts,
                                value_parts,
                                value_work,
                                value_others,
                                value_total,
                                notes,
                                notes_for_owner, 
                                operation_date_start,
                                operation_date_end,
                                delay,
                                comtec_id_status,
                                status_reg
                           ) VALUES (
                                 $this->comtec_id_order,
                                 $this->comtec_id_machine,
                                 $this->comtec_id_responsible,
                                 $this->operation_type,
                                '$this->diagnosis',
                                '$this->changed_parts',
                                 $this->value_parts,
                                 $this->value_work,
                                 $this->value_others,
                                 $this->value_total,
                                '$this->notes',
                                '$this->notes_for_owner',
                                '$this->operation_date_start',";
                                if ($this->operation_date_end == 0){
                                 $tsql .= "curdate(),";   
                                }else{
                                 $tsql .= "'$this->operation_date_end',"; 
                                }
                         
                                 $tsql .= "$this->delay,
                                           $this->status,
                                           $this->status_reg)";
        $stmt = $db->query($tsql);
        
        if($db->affected_rows() < 1){
            $this->errors[] = 'No se puede procesar la inscripci&oacute;n';
        }elseif($this->operation_type == 1){
              $tsql  = "UPDATE machines SET ";
	      $tsql .= " date_next_maintenance = '$this->date_next_maintenance' ";
	      $tsql .= " WHERE comtec_id_machine= $this->comtec_id_machine ";              
              $stmt = $db->query($tsql);
             }    
    }

    public function show_errors()
    {
        $msg = "";
        foreach($this->errors as $key=>$value)
            $msg = $msg . $value . "\n";
        return $msg;
    }

    public function valid_token()
    {
        if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
            $this->errors[] = 'Registro inv&aacute;lido';

        return count($this->errors)? 0 : 1;
    }
}
?>
